package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IStatementItemService statementItemService;


    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        //创建时间
        statement.setCreateTime(new Date());
        //初始化状态
        statement.setStatus(Statement.STATUS_CONSUME);
        statementMapper.insert(statement);
    }



    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {

        //1：必须是消费中
        Statement temp = this.get(statement.getId());
        if(!Statement.STATUS_CONSUME.equals(temp.getStatus())){
            throw new BusinessException("只有消费中状态才可以修改数据");
        }
        //2：改动中参数丢失问题
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public Statement generateStatement(Long appointmentId) {
        //预约单
        Appointment appointment = appointmentService.get(appointmentId);
        //添加结算单
        Statement statement = this.generate(appointment);
        statementMapper.insert(statement);
        return statement;
    }

    private Statement generate(Appointment appointment) {
        Statement statement = new Statement();
        statement.setCreateTime(new Date());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
        statement.setCarSeries(appointment.getCarSeries());//汽车类型
        statement.setCustomerName(appointment.getCustomerName());//客户名称
        statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
        statement.setAppointmentId(appointment.getId());//预约单ID
        statement.setServiceType(appointment.getServiceType() + 0L);//服务类型
        statement.setCustomerPhone(appointment.getCustomerPhone());//用户联系方式
        return statement;
    }

    @Override
    public Statement getByAppointmentId(Long appointmentId) {
        return statementMapper.selectByAppointmentId(appointmentId);
    }

    @Override
    public void updateAmount(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal price) {
        statementMapper.updateAmount(statementId, totalAmount, totalQuantity, price);
    }

    @Override
    public void payStatement(Long statementId) {
        //确认支付-支付成功
        Statement statement = this.get(statementId);
        //结算单- 收款时间， 收款人， 结算单状态：已支付
        statement.setPayTime(new Date());
        Long userId = ShiroUtils.getUserId();  //当前登录用户
        statement.setPayeeId(userId);  //收款人
        statement.setStatus(Statement.STATUS_PAID);
        statementMapper.updatePay(statement);
        //如果是预约结算单， 修改预约单状态
        if(statement.getAppointmentId() != null){
            appointmentService.changeStatus(statement.getAppointmentId(),
                    Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public void delete(Long id) {
        //加上状态判断
        Statement statement = this.get(id);
        if(Statement.STATUS_CONSUME.equals(statement.getStatus())){
            statementMapper.deleteByPrimaryKey(id);
            statementItemService.deleteByStatementId(id);
            //假设采用： 如果结算单被删除此时可以将预约记录改为用户取消
        }else{
            throw new BusinessException("只有在消费中才允许删除");
        }
    }
}
